package me.payge.video

import android.Manifest
import android.os.Bundle
import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat

class MainActivity : AppCompatActivity(), SurfaceHolder.Callback {

    private lateinit var mSurfaceView: SurfaceView
    private val mRecorder = VideoRecorder()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mSurfaceView = findViewById(R.id.surface)
        mSurfaceView.holder.addCallback(this)
        ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 1)
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {}

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        mRecorder.destroy()
    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        val surface = holder.surface
        val frame = holder.surfaceFrame
        Log.d("AvSdk", "surfaceCreated <- $frame")
        mRecorder.create(surface, frame.width(), frame.height())
    }
}
